package dongze.com.ckdj.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import dongze.com.ckdj.R;

/**
 * Created by fanqwz on 2017/7/24.
 */

public class MassLossPositionRadioGroup extends RadioGroup {
    int margin = 0;
    int radiobuttonWidth = 0;
    int radioButtonHeight = 0;

    public MassLossPositionRadioGroup(Context context) {
        super(context);
        init(context);
    }

    public MassLossPositionRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        margin = context.getResources().getDimensionPixelSize(R.dimen.mass_loss_ratio_button_margin);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        RadioButton button = (RadioButton) getChildAt(0);
        radiobuttonWidth = button.getMeasuredWidth();
        radioButtonHeight = button.getMeasuredHeight();
        setMeasuredDimension(button.getMeasuredWidth() * 3 + margin * 4, button.getMeasuredHeight() * 3 + margin * 4);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
        if (changed) {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    int left = (j + 1) * margin + radiobuttonWidth * j;
                    int top = (i + 1) * margin + radioButtonHeight * i;
                    RadioButton radioButton = (RadioButton) getChildAt(i * 3 + j);
                    radioButton.layout(left, top, left + radiobuttonWidth, top + radioButtonHeight);
                }
            }
        }
    }
}
